package com.datamation.swadeshi.control.data;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.datamation.swadeshi.model.FTranHed;
import com.datamation.swadeshi.model.ItemLoc;
import com.datamation.swadeshi.model.Items;

public class ItemLocDS {

	private SQLiteDatabase dB;
	private DatabaseHelper dbHelper;
	Context context;
	private String TAG="swadeshi";
	
	
	public ItemLocDS (Context context){
		this.context = context;
		dbHelper = new DatabaseHelper(context);
	}
	
	public void open() throws SQLException {
		dB = dbHelper.getWritableDatabase();
	}
	
	@SuppressWarnings("static-access")
	public int createOrUpdateItemLoc(ArrayList<ItemLoc>  list) {
		
		int serverdbID =0;
		
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		
		
	try{
		
					
    for (ItemLoc loc : list) {
    	
    	
    	ContentValues values = new ContentValues();

		
		
    	
    		String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FITEMLOC
					+ " WHERE " + dbHelper.FITEMLOC_ITEM_CODE+ " = '" +loc.getFITEMLOC_ITEM_CODE()+ "' AND " + dbHelper.FITEMLOC_LOC_CODE+ " = '" +loc.getFITEMLOC_LOC_CODE()+ "' ";
			cursor = dB.rawQuery(selectQuery, null);
			
			values.put(dbHelper.FITEMLOC_ITEM_CODE, loc.getFITEMLOC_ITEM_CODE());
			values.put(dbHelper.FITEMLOC_LOC_CODE, loc.getFITEMLOC_LOC_CODE());
			values.put(dbHelper.FITEMLOC_QOH, loc.getFITEMLOC_QOH());
			values.put(dbHelper.FITEMLOC_RECORD_ID, loc.getFITEMLOC_RECORD_ID());
			
			int count = cursor.getCount();
			if(count>0)
			{
				serverdbID = dB.update(dbHelper.TABLE_FITEMLOC, values,dbHelper.FITEMLOC_ITEM_CODE + " =?" + " AND " + dbHelper.FITEMLOC_LOC_CODE + " =?",	
						new String[] { String.valueOf(loc.getFITEMLOC_ITEM_CODE()),String.valueOf(loc.getFITEMLOC_LOC_CODE())});
				
			}
			else 
			{
				serverdbID = (int) dB.insert(dbHelper.TABLE_FITEMLOC, null, values);

			}
			
		
			}
		}catch (Exception e) {
		
			Log.v(TAG+" Exception", e.toString());
	
		}finally {  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		return serverdbID;
				
	}

	
	@SuppressWarnings("static-access")
	public ArrayList<ItemLoc> getAllItemLoc() {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<ItemLoc> list = new ArrayList<ItemLoc>();
		
		String selectQuery ="SELECT * FROM "+dbHelper.TABLE_FITEMLOC +" WHERE "+ dbHelper.FITEMLOC_LOC_CODE+" ='MS'"; 
		
		Cursor cursor = dB.rawQuery(selectQuery, null);
		while(cursor.moveToNext()){
			
			ItemLoc itemLoc=new ItemLoc();
		
			itemLoc.setFITEMLOC_ID(cursor.getString(cursor.getColumnIndex(dbHelper.FITEMLOC_ID)));
			itemLoc.setFITEMLOC_ITEM_CODE(cursor.getString(cursor.getColumnIndex(dbHelper.FITEMLOC_ITEM_CODE)));
			itemLoc.setFITEMLOC_LOC_CODE(cursor.getString(cursor.getColumnIndex(dbHelper.FITEMLOC_LOC_CODE)));
			itemLoc.setFITEMLOC_QOH(cursor.getString(cursor.getColumnIndex(dbHelper.FITEMLOC_QOH)));
			itemLoc.setFITEMLOC_RECORD_ID(cursor.getString(cursor.getColumnIndex(dbHelper.FITEMLOC_RECORD_ID)));
			
			list.add(itemLoc);
			
		}
		
		return list;
	}
	
	
	public int deleteAllItemLoc(){
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		try{
			
			cursor = dB.rawQuery("SELECT * FROM " + dbHelper.TABLE_FITEMLOC, null);
			count =cursor.getCount();
			if(count>0){
				int success = dB.delete(dbHelper.TABLE_FITEMLOC, null, null);
				Log.v("Success", success+"");
			}
		}catch (Exception e){

			Log.v(TAG+" Exception", e.toString());
			
		}finally{  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		
		return count;
		
	}
	
	public String getAllitemloc(String Itemcode,String Loccode) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		String list = null;
		
		
		@SuppressWarnings("static-access")
		String selectQuery = "select QOH from FItemLoc where Itemcode = '"+Itemcode+"' AND LocCode ='"+Loccode+"' ";
		
	
		Cursor cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){
			
			ItemLoc loc =new ItemLoc();
		
			//loc.setFITEMLOC_ITEM_CODE(cursor.getString(cursor.getColumnIndex(dbHelper.FITEMLOC_ITEM_CODE)));
			//loc.setFITEMLOC_LOC_CODE(cursor.getString(cursor.getColumnIndex(dbHelper.FITEMLOC_LOC_CODE)));
			loc.setFITEMLOC_QOH(cursor.getString(cursor.getColumnIndex(dbHelper.FITEMLOC_QOH)));
			
	
			list = cursor.getString(0);
			
		}
		
		return list;
	}
	@SuppressWarnings("static-access")
	public int AddnewRecord(ArrayList<ItemLoc>  list) {
		
		int serverdbID =0;
		int serverdbID1 =0;
		
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		
		
	try{
		
					
    for (ItemLoc loc : list) {
    	
    	
    	ContentValues values = new ContentValues();
    	ContentValues values1 = new ContentValues();
    	
    	//if(loc.getFITEMLOC_LOC_CODE().split("-:-")[0].toString().equals(loc.getFITEMLOC_LOC_CODE().split("-:-")[1].toString()))
    	//{
    		String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FITEMLOC
			+ " WHERE " + dbHelper.FITEMLOC_ITEM_CODE + " = '" +loc.getFITEMLOC_ITEM_CODE()+ "' AND " + dbHelper.FITEMLOC_LOC_CODE+ " = '"+loc.getFITEMLOC_LOC_CODE().split("-:-")[1].toString()+"'";

    		
			cursor = dB.rawQuery(selectQuery, null);

			values.put(dbHelper.FITEMLOC_QOH, loc.getFITEMLOC_QOH().split("-:-")[0].toString());
			values.put(dbHelper.FITEMLOC_LOC_CODE, loc.getFITEMLOC_LOC_CODE().split("-:-")[0].toString());
			
			values1.put(dbHelper.FITEMLOC_QOH, loc.getFITEMLOC_QOH().split("-:-")[1].toString());
			values1.put(dbHelper.FITEMLOC_LOC_CODE, loc.getFITEMLOC_LOC_CODE().split("-:-")[1].toString());
			
			int count = cursor.getCount();
			if(count>0)
			{
				serverdbID = dB.update(dbHelper.TABLE_FITEMLOC,values,dbHelper.FITEMLOC_ITEM_CODE + " = '" +loc.getFITEMLOC_ITEM_CODE()+ "' " + " AND " + dbHelper.FITEMLOC_LOC_CODE + " ='"+loc.getFITEMLOC_LOC_CODE().split("-:-")[0].toString()+"'",	
						null);
				serverdbID1 = dB.update(dbHelper.TABLE_FITEMLOC,values1,dbHelper.FITEMLOC_ITEM_CODE + " = '" +loc.getFITEMLOC_ITEM_CODE()+ "' " + " AND " + dbHelper.FITEMLOC_LOC_CODE + " ='"+loc.getFITEMLOC_LOC_CODE().split("-:-")[1].toString()+"'",	
						null);

				
			}

    	}
    		
	
		
		//	}
		}catch (Exception e) {
		
			Log.v(TAG+" Exception", e.toString());
	
		}finally {  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		return serverdbID;
				
	}

}
